﻿using System;
using System.Net;
using System.Collections.Generic;
using SCG.General;

namespace SCG.SolarVengeanceEngine.StarShips
{
    //BattleStations
    public class BattleStation : ResourceStarShip
    {
        //constructor
        public BattleStation(SVGame game, Player owner, int hull, StarSystem origin)
            : base(game, owner, 6, hull, origin)
        {
        }

        //ship name
        public override string Name
        {
            get
            {
                return "BattleStation";
            }
        }

        //description
        public override string Description
        {
            get 
            {
                return "BattleStations have a Hull rating that absorbs damage points, and they can slowly repair themselves and other adjacent damaged StarShips.  BattleStations can attack StarSystems and reduce Shields by 1 each time they attack.  BattleStations scoop Resources as they move, and they can build other StarShips with their accumulated Resources.";
            }
        }

        //ship type
        public override StarShipType ShipType
        {
            get
            {
                return StarShipType.BattleStation;
            }
        }

        //can conquer
        public override bool CanConquerStarSystems
        {
            get
            {
                return true;
            }
        }

        //they can execute a number of orders
        public override bool CanExecuteOrder(OrderType ot)
        {
            switch (ot)
            {
                case OrderType.TargetStarSystem:
                    return true;
                case OrderType.Unload:
                case OrderType.ShipBuildStarShip:
                    return ResourcesInt > 0;                
                default:
                    return base.CanExecuteOrder(ot);
            }
        }

        //can serve as a transport source and destination
        public override bool IsTransportSource
        {
            get
            {
                return true;
            }
        }
        public override bool IsTransportDestination
        {
            get
            {
                return true;
            }
        }

        //Show resources when moving
        public override string PrimarySystemMark
        {
            get
            {
                if (!DiscoveredByCurrentPlayer)
                    return "";
                else if (ResourcesInt > 0)
                    return base.PrimarySystemMark + " R" + ResourcesInt;
                else
                    return base.PrimarySystemMark;
            }
        }

        //BattleStations reduce shields by 1 when attacking starsystems
        protected override void TargetAdjacentStarSystem(StarSystem system)
        {
            //Report the attack to the host
            if (Game.Host != null)
                Game.Host.StarSystemAttacked(system, Owner);

            //Submit state change
            Game.SubmitStateChange(StateChange.AttackStarSystem, system, Owner);  
   
            //reduce shields, conquer if zero
            if (system.Shields <= 0)
            {
                Game.ConquerStarSystem(Owner, system);
            }
            else
            {
                system.Shields--;
                Game.SubmitStateChange(StateChange.SetShields, system, system.Shields);
            }
        }

        //Perform repair every other move
        protected internal override bool MoveProcessing()
        {
            if (Game.Impulse % 2 == 0)
            {
                //if damaged, repair itself
                if (IsDamaged)
                    Game.RepairStarShip(this);
                else
                {
                    //Repair an adjacent friendly ship
                    List<SCGPoint> adj = Game.AdjacentPoints(Location, false);
                    foreach (SCGPoint pt in adj)
                    {
                        SVMapObject svm = Game.GetObjectAt((int)pt.X, (int)pt.Y);
                        if (svm is StarShip)
                        {
                            StarShip damaged = svm as StarShip;
                            if (damaged.IsDamaged)
                                if (Owner.FriendlyTo(damaged.Owner))
                                {
                                    Game.RepairStarShip(damaged);
                                    break;
                                }
                        }
                    }
                }
            }
            return false;
        }

        //Scoop Resources after a move
        protected internal override void PostMove(int oldX, int oldY)
        {
            Resources += 0.25d * Owner.TechMultiple;
            Game.SubmitStateChange(StateChange.SetShipResources, this, Resources);
        }

        //Always damage hull over engines
        protected internal override void ProcessDamage(int damage, bool canDamageEngines)
        {
            if (damage >= Value)
                if (damage < Engines)
                {
                    DamageEngines(damage);
                    return;
                }
            DamageExtra(damage);
        }
    }
}
